﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCClubSite.IDAL;
using MVCClubSite.Model;

namespace MVCClubSite.LinqToSQLDAL
{
    /// <summary>
    /// Linq To Sql Images DAL
    /// Decided not to load the images on the gets to save memory.
    /// We are using the Image controller to display images.
    /// </summary>
    public class Images : IImages 
    {
         DB _db;

        /// <summary>
        /// Default constructor
        /// </summary>
        public Images()
        {
            _db = new DB();
        }

        /// <summary>
        /// Constructor used to pass in a datacontext
        /// </summary>
        /// <param name="db">datacontext for the DAL to use</param>
        public Images(DB db)
        {
            _db = db;
            if (_db == null)
                throw new InvalidOperationException("DB cannot be null.");
        }

        /// <summary>
        /// Method to get Images
        /// </summary>
        /// <returns>A list of Images</returns>
        public IQueryable<Image> GetImages()
        {
            return from i in _db.cs_Images
                         select new Image
                         {
                             ID = i.ImageID,
                             AlbumID = i.AlbumID,
                             Description = i.Description,
                         };
        }

        /// <summary>
        /// Method to an albums images
        /// </summary>
        /// <param name="id">ID of the album</param>
        /// <returns>A list of Images</returns>
        public IQueryable<Image> GetImagesByAlbumID(int id)
        {
            return from i in _db.cs_Images
                   where i.AlbumID == id
                   select new Image
                   {
                       ID = i.ImageID,
                       AlbumID = i.AlbumID,
                       Description = i.Description,
                   };
        }

        /// <summary>
        /// Method to get image ids of all images in an album
        /// </summary>
        /// <param name="id">ID of the album</param>
        /// <returns>A list of integers</returns>
        public IQueryable<int> GetImageIDsByAlbumID(int id)
        {
            return from i in _db.cs_Images
                   where i.AlbumID == id
                   select i.ImageID;
        }

        /// <summary>
        /// Method to get an image id for an album thumbnail
        /// </summary>
        /// <param name="albumID">ID of the album</param>
        /// <returns>ID of the image</returns>
        public int GetAlbumThumbnailID(int albumID)
        {
            return (from i in _db.cs_Images
                    where i.AlbumID == albumID
                    select i.ImageID).FirstOrDefault();
        }

        /// <summary>
        /// Method to get an image by id
        /// </summary>
        /// <param name="id">ID of the image to return</param>
        /// <returns>An image business entity</returns>
        public Image GetImageByID(int id)
        {
            return (from i in _db.cs_Images
                    where i.ImageID == id
                    select new Image
                    {
                        ID = i.ImageID,
                        AlbumID = i.AlbumID,
                        Description = i.Description,
                        OriginalImage = i.OriginalImage.ToArray(),
                        LargeImage = i.LargeImage.ToArray(),
                        ThumbnailImage = i.ThumbnailImage.ToArray()
                    }).SingleOrDefault();
        }

        /// <summary>
        /// Method to insert or update an image
        /// </summary>
        /// <param name="image">Image to insert or update</param>
        public void Save(Image image)
        {
            using (DB db = new DB())
            {
                cs_Image dbImage = db.cs_Images.Where(i => i.ImageID == image.ID).SingleOrDefault();
                bool isNew = false;
                if (dbImage == null)
                {
                    dbImage = new cs_Image();
                    isNew = true;
                }

                dbImage.AlbumID = image.AlbumID;
                dbImage.Description = image.Description;
                dbImage.OriginalImage = image.OriginalImage;
                dbImage.LargeImage = image.LargeImage;
                dbImage.ThumbnailImage = image.ThumbnailImage;

                if (isNew)
                    db.cs_Images.InsertOnSubmit(dbImage);

                db.SubmitChanges();
            }
        }

        /// <summary>
        /// Method to delete an image
        /// </summary>
        /// <param name="id"></param>
        public void Delete(int id)
        {
            cs_Image dbImage = _db.cs_Images.Where(i => i.ImageID == id).SingleOrDefault();

            if (dbImage != null)
            {
                _db.cs_Images.DeleteOnSubmit(dbImage);
                _db.SubmitChanges();
            }
        }

        /// <summary>
        /// Method to delete all images in an album
        /// </summary>
        /// <param name="ID">ID of the album</param>
        public void DeleteByAlbumID(int albumID)
        {
            var dbImages = _db.cs_Images.Where(i => i.AlbumID == albumID);

            if (dbImages != null)
            {
                _db.cs_Images.DeleteAllOnSubmit(dbImages);
                _db.SubmitChanges();
            }
        }
    }
}
